<template>
	<view>
		<view v-for="item in currentClassMessages" style="width: 300px;height: 200px;background: #4CD964;margin-top: 50px;border-radius: 15px 15px 15px 15px;">
			<image :src="item.classImage" style="border-radius: 50%;width: 50px;height: 50px;"></image>
				<text>
					 {{item.username}}\n
					{{item.message}}
				</text>
			<button v-if="item.isread==0" style="width: 100px;height: 50px;margin-top: 50px;margin-left: 10px;" @click="confirm(item)">确认</button>
		</view>
		
		<view style="margin-top: 50px;">
			<input style="width: 300px;height: 50px;background-color: #808080;float:left" v-model="message"/><button style="width: 100px;height: 50px;" @click="sendMsg()">发送</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentClassMessages:[],
				message:""
			}
		},
		onShow() {
			var currentClassMessages=uni.getStorageSync("currentClassMessages");
			this.currentClassMessages=currentClassMessages;
		}
		,
		methods: {
			confirm(item){
				var id=item.id;
				uni.request({
				method:"GET",
				url:this.requestUrl+"/ForeClassMsg/changeRead?id="+id,
				success:response=>{
					item.isread=0;
				}	
				})
				
			},
			sendMsg(){
				var user=uni.getStorageSync("user");
				var classMessage={};
				classMessage.isread=3;
				classMessage.userId=user.id;
				classMessage.classImage=user.image;
				classMessage.message=this.message;
				classMessage.classId=uni.getStorageSync("currentClassId");
				uni.request({
				method:"POST",
				url:this.requestUrl+"/ForeClassMsg/sendMsg",
				data:classMessage,
				success:response=>{
					this.currentClassMessages.unshift(classMessage);
				}	
				})
			}
		}
	}
</script>

<style>

</style>
